//
//  QQGoodsAdressVC.m
//  QQZF
//
//  Created by hdz on 2018/7/2.
//  Copyright © 2018年 com.szqqkj. All rights reserved.
//

#import "QQGoodsAdressVC.h"
#import "QQGoodsAdressCell.h"
#import "HGAddressModel.h"
#import "QQNewAddressVC.h"

@interface QQGoodsAdressVC ()<UITableViewDelegate, UITableViewDataSource,QQGoodsAdressCellDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray <HGAddressModel *>*array;

@property (nonatomic,strong)UIButton *addAddressBtn;

@end

@implementation QQGoodsAdressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title=@"收货地址";
    
    [self initSubViews];
    
    [self.view setBackgroundColor:kColorBackground];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}

//1000 编辑 1001删处
- (void)actionModel:(HGAddressModel *)model withTag:(NSInteger)tag{
    if (tag == 999) {
        [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/user/address/modify/default") parameters:@{@"id" : [NSNumber numberWithInteger:model.addressID]} completion:^(LMJBaseResponse *response) {
            if (response.responseModel.status == 1) {
                [[XNProgressHUD shared] showSuccessWithTitle:response.responseModel.message maskType:(XNProgressHUDMaskTypeBlack)];
                [self getData];
            }
        }];
    } else if (tag == 1000){
        QQNewAddressVC *VC = [[QQNewAddressVC alloc] init];
        VC.model = model;
        [self.navigationController pushViewController:VC animated:YES];
    } else {
        LEEAlert.alert.config.LeeTitle(@"删除地址").LeeContent(@"确认删处该收货地址？").LeeCancelAction(@"取消", nil).LeeAction(@"确认", ^{
            
            [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/user/address/del") parameters:@{@"id" : [NSNumber numberWithInteger:model.addressID]} completion:^(LMJBaseResponse *response) {
                if (response.responseModel.status == 1) {
                    [[XNProgressHUD shared] showSuccessWithTitle:response.responseModel.message maskType:(XNProgressHUDMaskTypeBlack)];
                    [self getData];
                }
            }];
            
        }).LeeShow();
    }
}

-(void)getData {
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/user/address/list") parameters:nil completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            _array = [HGAddressModel mj_objectArrayWithKeyValuesArray:response.responseModel.data];
            [_tableView reloadData];
        }
    }];
}
#pragma mark - Interactive
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QQGoodsAdressCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndefier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.model = _array[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

#pragma mark - Init
-(void)initSubViews {
    [self.tableView registerClass:[QQGoodsAdressCell class] forCellReuseIdentifier:cellIndefier];
}

- (void)newAddress {
    [self.navigationController pushViewController:[[QQNewAddressVC alloc] init] animated:YES];
}

-(UITableView *)tableView {
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 110;
        _tableView.backgroundColor=kColorBackground;
        _tableView.tableFooterView = [UIView new];
//        _tableView.placeholderText = @"亲，你还没有地址哦~";
//        _tableView.placeholderImageName = @"adress";
        [self.view addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(kNavigationHeight + 1);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.addAddressBtn.mas_top);
        }];
    }
    return _tableView;
}

- (UIButton *)addAddressBtn {
    if (!_addAddressBtn) {
        _addAddressBtn = [[UIButton alloc] init];
        [_addAddressBtn setBackgroundColor:kColorMainTheme];
        [_addAddressBtn setTitle:@"新增地址" forState:(UIControlStateNormal)];
        [_addAddressBtn addTarget:self action:@selector(newAddress) forControlEvents:(UIControlEventTouchUpInside)];
        [self.view addSubview:_addAddressBtn];
        
        [_addAddressBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.height.offset(45);
            make.bottom.offset(-kTabbarSafeBottomMargin);
        }];
    }
    return _addAddressBtn;
}

-(NSMutableArray <HGAddressModel *>*)array
{
    if (!_array)
    {
        self.array=[NSMutableArray array];
    }
    return _array;
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // 按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    //标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    
    [self getData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    // 按钮颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    // 导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
}

@end
